package tw.com.softleader.JGB.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import tw.com.softleader.JGB.dao.GuestBookDao;
import tw.com.softleader.JGB.model.GuestBookModel;

public class GuestBookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);

	}
 
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		GuestBookDao gBD = new GuestBookDao();

		if ("add".equals(request.getParameter("action"))) {

			// get data from req
			GuestBookModel model = new GuestBookModel();
			String name = request.getParameter("name");
			String title = request.getParameter("title");
			String content = request.getParameter("content");

			model.setName(name);
			model.setTitle(title);
			model.setContent(content);

			// call dao
			gBD.insert(model);

			// dispatch
			
			List<GuestBookModel> list = gBD.select();
			request.setAttribute("guestBookList", list);
			request.getRequestDispatcher("list.jsp").forward(request, response);
			
		} else {
			List<GuestBookModel> list = gBD.select();
			request.setAttribute("guestBookList", list);
			request.getRequestDispatcher("list.jsp").forward(request, response);
		}

	}

	
	
}
